#include "work.h"
#include "ui.h"
#include "stdio.h"
#include "io.h"
#include "cmsis_os.h"
#include "motor.h"

WORK work;




void work_init()
{
	work.work_mode=MODE_STEP;
	lv_obj_clear_state(ui_CheckboxNormalMode , LV_STATE_CHECKED);
	lv_obj_add_state(ui_CheckboxStepMode , LV_STATE_CHECKED);

	work.work_speed=SPEED_MID;
    lv_obj_set_style_bg_color(ui_ButtonLowSpeed, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_LabelLowSpeed, lv_color_hex(0x4193D0), LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_set_style_bg_color(ui_ButtonMidSpeed, lv_color_hex(0x4193D0), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_LabelMidSpeed, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_set_style_bg_color(ui_ButtonHighSpeed, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui_LabelHighSpeed, lv_color_hex(0x4193D0), LV_PART_MAIN | LV_STATE_DEFAULT);


	work.work_time=DEFAULT_WORK_TIME;
	char info[5]={0};
	sprintf(info,"%d",work.work_time);
	lv_label_set_text(ui_LabelSetTime, info);

	work.work_group=GROUP_2X;
	lv_obj_add_state(ui_CheckboxGroup2X , LV_STATE_CHECKED);
	lv_obj_clear_state(ui_CheckboxGroup4X , LV_STATE_CHECKED);

	lv_obj_add_flag(ui_ButtonGroup41, LV_OBJ_FLAG_HIDDEN);
	lv_obj_add_flag(ui_ButtonGroup42, LV_OBJ_FLAG_HIDDEN);

	lv_obj_clear_flag(ui_ButtonGroup21, LV_OBJ_FLAG_HIDDEN);
	lv_obj_clear_flag(ui_ButtonGroup22, LV_OBJ_FLAG_HIDDEN);
	lv_obj_clear_flag(ui_ButtonGroup23, LV_OBJ_FLAG_HIDDEN);
	lv_obj_clear_flag(ui_ButtonGroup24, LV_OBJ_FLAG_HIDDEN);

	work.group_state.group_21=DISABLE;
	work.group_state.group_22=DISABLE;
	work.group_state.group_23=DISABLE;
	work.group_state.group_23=DISABLE;
	work.group_state.group_41=DISABLE;
	work.group_state.group_42=DISABLE;

    lv_obj_set_style_bg_img_src(ui_ButtonGroup21, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup22, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup23, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup24, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup41, &ui_img_1214365082, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup42, &ui_img_1214365082, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_label_set_text(ui_LabelTimeCount, "---");
    lv_obj_add_flag(ui_ButtonTimeCount, LV_OBJ_FLAG_HIDDEN);

    work.work_state=STOP;
}






void Clear_All_Clickable_Flag()
{
	lv_obj_clear_flag(ui_CheckboxStepMode, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_CheckboxNormalMode, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_ButtonLowSpeed, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonMidSpeed, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonHighSpeed, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_ButtonDecrease, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonIncrease, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_CheckboxGroup2X, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_CheckboxGroup4X, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_ButtonGroup21, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonGroup22, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonGroup23, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonGroup24, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_ButtonGroup41, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonGroup42, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_clear_flag(ui_ButtonStart, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_clear_flag(ui_ButtonStop, LV_OBJ_FLAG_CLICKABLE);
}

void Add_All_Clickable_Flag()
{
	lv_obj_add_flag(ui_CheckboxStepMode, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_CheckboxNormalMode, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_ButtonLowSpeed, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonMidSpeed, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonHighSpeed, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_ButtonDecrease, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonIncrease, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_CheckboxGroup2X, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_CheckboxGroup4X, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_ButtonGroup21, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonGroup22, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonGroup23, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonGroup24, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_ButtonGroup41, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonGroup42, LV_OBJ_FLAG_CLICKABLE);

	lv_obj_add_flag(ui_ButtonStart, LV_OBJ_FLAG_CLICKABLE);
	lv_obj_add_flag(ui_ButtonStop, LV_OBJ_FLAG_CLICKABLE);
}







uint16_t count_time()
{
	uint16_t time=0;
	if(work.work_group==GROUP_2X)
	{
		if(work.group_state.group_21==ENABLE)
		{
			time+=work.work_time;
		}
		if(work.group_state.group_22==ENABLE)
		{
			time+=work.work_time;
		}
		if(work.group_state.group_23==ENABLE)
		{
			time+=work.work_time;
		}
		if(work.group_state.group_24==ENABLE)
		{
			time+=work.work_time;
		}
	}
	else if(work.work_group==GROUP_4X)
	{
		if(work.group_state.group_41==ENABLE)
		{
			time+=work.work_time;
		}
		if(work.group_state.group_42==ENABLE)
		{
			time+=work.work_time;
		}
	}
	return time;
}


void work_stop()
{
	motor_set_speed(1, 0);
	motor_set_speed(2, 0);
	motor_set_speed(3, 0);
	motor_set_speed(4, 0);
	motor_set_speed(5, 0);
	motor_set_speed(6, 0);
	motor_set_speed(7, 0);
	motor_set_speed(8, 0);

	osDelay(POWER_DELAY);
	power_off();
	osDelay(POWER_DELAY);

    work.group_state.group_21=DISABLE;
    work.group_state.group_22=DISABLE;
    work.group_state.group_23=DISABLE;
    work.group_state.group_24=DISABLE;
    work.group_state.group_41=DISABLE;
    work.group_state.group_42=DISABLE;

    lv_obj_set_style_bg_img_src(ui_ButtonGroup21, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup22, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup23, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup24, &ui_img_1214362784, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup41, &ui_img_1214365082, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_img_src(ui_ButtonGroup42, &ui_img_1214365082, LV_PART_MAIN | LV_STATE_DEFAULT);

	lv_label_set_text(ui_LabelTimeCount, "---");
    lv_obj_add_flag(ui_ButtonTimeCount, LV_OBJ_FLAG_HIDDEN);

    work.work_state=STOP;

    Add_All_Clickable_Flag();	//使能所有触摸
}


uint8_t work_delay(uint8_t delay_time,uint32_t *display_time)
{
	for(int i=0;i<delay_time*10;i++)
	{
		if(work.work_state==RUN)
		{
			osDelay(100);
			if(i>0&&i%10==0)
			{
				char count_time_display[10];
				*display_time-=1000;
				sprintf(count_time_display,"%.0f",(float)(*display_time/1000));
				lv_label_set_text(ui_LabelTimeCount, count_time_display);
			}
		}
		else {
			//停止
			 work_stop();
			 return 1;
		}
	}
	return 0;
}



void work_grind()
{
	if(work.work_group==GROUP_2X||work.work_group==GROUP_4X)
	{
		uint16_t speed;
		uint32_t display_time=count_time()*1000;
		work.work_state=RUN;

		switch(work.work_speed)
		{
			case SPEED_LOW 	: speed=LOW_SPEED_PWM;break;
			case SPEED_MID 	: speed=MID_SPEED_PWM;break;
			case SPEED_HIGH : speed=HIGH_SPEED_PWM;break;
			default			: speed=0;break;
		}

		lv_label_set_text(ui_LabelTimeCount, "---");
		lv_obj_clear_flag(ui_ButtonTimeCount, LV_OBJ_FLAG_HIDDEN);//显示倒计时

		if(work.work_group==GROUP_2X&&(work.group_state.group_21||work.group_state.group_22||work.group_state.group_23||work.group_state.group_24))
		{
			osDelay(POWER_DELAY);
			power_on();
			osDelay(POWER_DELAY);

			char count_time_display[10];
			sprintf(count_time_display,"%.0f",(float)(display_time/1000));
			lv_label_set_text(ui_LabelTimeCount, count_time_display);

			if(work.group_state.group_21==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(1, speed);
					motor_set_speed(2, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(1, 0);
					motor_set_speed(2, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(1, (uint16_t)speed_step);
					motor_set_speed(2, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(1, speed_step);
						motor_set_speed(2, speed_step);
					}

					motor_set_speed(1, speed);
					motor_set_speed(2, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(1, 0);
					motor_set_speed(2, 0);
				}
			}

			if(work.group_state.group_22==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(3, speed);
					motor_set_speed(4, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(3, 0);
					motor_set_speed(4, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(3, (uint16_t)speed_step);
					motor_set_speed(4, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(3, speed_step);
						motor_set_speed(4, speed_step);
					}

					motor_set_speed(3, speed);
					motor_set_speed(4, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(3, 0);
					motor_set_speed(4, 0);
				}
			}

			if(work.group_state.group_23==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(5, speed);
					motor_set_speed(6, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(5, 0);
					motor_set_speed(6, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(5, (uint16_t)speed_step);
					motor_set_speed(6, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(5, speed_step);
						motor_set_speed(6, speed_step);
					}

					motor_set_speed(5, speed);
					motor_set_speed(6, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(5, 0);
					motor_set_speed(6, 0);
				}
			}

			if(work.group_state.group_24==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(7, speed);
					motor_set_speed(8, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(7, 0);
					motor_set_speed(8, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(7, (uint16_t)speed_step);
					motor_set_speed(8, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(7, speed_step);
						motor_set_speed(8, speed_step);
					}

					motor_set_speed(7, speed);
					motor_set_speed(8, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(7, 0);
					motor_set_speed(8, 0);
				}
			}

			lv_label_set_text(ui_LabelTimeCount, "---");

			osDelay(POWER_DELAY);
			power_off();
			osDelay(POWER_DELAY);
		}
		else if(work.work_group==GROUP_4X&&(work.group_state.group_41||work.group_state.group_42))
		{
			osDelay(POWER_DELAY);
			power_on();
			osDelay(POWER_DELAY);

			char count_time_display[10];
			sprintf(count_time_display,"%.0f",(float)(display_time/1000));
			lv_label_set_text(ui_LabelTimeCount, count_time_display);

			if(work.group_state.group_41==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(1, speed);
					motor_set_speed(2, speed);
					motor_set_speed(3, speed);
					motor_set_speed(4, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(1, 0);
					motor_set_speed(2, 0);
					motor_set_speed(3, 0);
					motor_set_speed(4, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(1, (uint16_t)speed_step);
					motor_set_speed(2, (uint16_t)speed_step);
					motor_set_speed(3, (uint16_t)speed_step);
					motor_set_speed(4, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(1, speed_step);
						motor_set_speed(2, speed_step);
						motor_set_speed(3, speed_step);
						motor_set_speed(4, speed_step);
					}

					motor_set_speed(1, speed);
					motor_set_speed(2, speed);
					motor_set_speed(3, speed);
					motor_set_speed(4, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(1, 0);
					motor_set_speed(2, 0);
					motor_set_speed(3, 0);
					motor_set_speed(4, 0);
				}
			}

			if(work.group_state.group_42==1)
			{
				if(work.work_mode==MODE_NORMAL)
				{
					motor_set_speed(5, speed);
					motor_set_speed(6, speed);
					motor_set_speed(7, speed);
					motor_set_speed(8, speed);
					if(work_delay(work.work_time,&display_time)==1) return;
					motor_set_speed(5, 0);
					motor_set_speed(6, 0);
					motor_set_speed(7, 0);
					motor_set_speed(8, 0);
				}else if (work.work_mode==MODE_STEP) {
					uint16_t speed_step=(float)START_SPEED_PWM;
					uint16_t step=((float)(speed-START_SPEED_PWM))/100.0f;
					motor_set_speed(5, (uint16_t)speed_step);
					motor_set_speed(6, (uint16_t)speed_step);
					motor_set_speed(7, (uint16_t)speed_step);
					motor_set_speed(8, (uint16_t)speed_step);
					for(int i=0;i<100;i++)
					{
						if(work.work_state==RUN)
						{
							osDelay(100);
							if(i>0&&i%10==0)
							{
								char count_time_display[10];
								display_time-=1000;
								sprintf(count_time_display,"%.0f",(float)(display_time/1000));
								lv_label_set_text(ui_LabelTimeCount, count_time_display);
							}
						}else {
							work_stop();
							return;
						}
						speed_step+=step;
						motor_set_speed(5, speed_step);
						motor_set_speed(6, speed_step);
						motor_set_speed(7, speed_step);
						motor_set_speed(8, speed_step);
					}

					motor_set_speed(5, speed);
					motor_set_speed(6, speed);
					motor_set_speed(7, speed);
					motor_set_speed(8, speed);
					if(work_delay(work.work_time-10,&display_time)==1) return;
					motor_set_speed(5, 0);
					motor_set_speed(6, 0);
					motor_set_speed(7, 0);
					motor_set_speed(8, 0);
				}
			}

			osDelay(POWER_DELAY);
			power_off();
			osDelay(POWER_DELAY);
		}
	}

	work_stop();
}























